package com.bite.demo.controller;

import com.bite.demo.model.userInfo;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/request")
@RestController
public class RequestController {

    @RequestMapping("/param1")
    public String param1(String userName) {
        System.out.println("接收到参数：" +userName );
        return "接收到参数1："+userName;
    }

    @RequestMapping("/param2")
    public String param2(Integer age) {
        return "接收到参数2："+age;
    }
    @RequestMapping("/param3")
    public String param3(int age) {
        return "接收到参数3："+age;
    }

    @RequestMapping("/param4")
    public String param4(String userName,String password) {
        return "用户名："+userName+"密码："+password;
    }
    @RequestMapping("/param5")
    public String param5(userInfo userInfo) {
        System.out.println(userInfo.getUserName());
        return "userInfo："+userInfo;
    }

    @RequestMapping("/param6")
    public String param6(@RequestParam(value = "userName",required = false) String name, String password) {
        return "用户名："+name+"密码："+password;
    }

    @RequestMapping("param7")
    public String param7(String[] str) {
        return "数组：" + Arrays.toString(str);
    }

    @RequestMapping("param8")
    public String param8(@RequestParam("list") List<String> list) {
        return "接收到的集合："+list;
    }

    @RequestMapping("/param9")
    public String param9(@RequestBody userInfo userinfo) {
        return "接收到参数userinfo："+userinfo;
    }

    @RequestMapping("/param10/{articleId}")
    public String param10(@PathVariable("articleId") Integer articleId) {
        return "接收到参数articleId:" + articleId;
    }

    @RequestMapping("param11")
    public String param11(@RequestPart("file1") MultipartFile file) throws IOException {
        System.out.println(file.getName());
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File(("D:/课堂任务/")+file.getOriginalFilename()));
        return "接收到文件：" + file.getOriginalFilename();
    }

    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        System.out.println("接收到Cookie：");
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + cookie.getValue());
            }
        }
        return "获取Cookie成功";
    }
    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("bite") String bite){
        System.out.println("bite" + bite);
        return "获取Cookie成功";
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        session.setAttribute("name","zhangsan");
        session.setAttribute("class","java111");
        return "设置Session";
    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        if(session!=null) {
            System.out.println(session.getAttribute("name"));
            System.out.println(session.getAttribute("class"));
        }
        return "获取Session成功";
    }
    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session) {
            System.out.println(session.getAttribute("name"));
            System.out.println(session.getAttribute("class"));
        return "获取Session成功";
    }

    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute(value = "name",required = false) String name) {
        System.out.println("name:"+ name);
        return "获取Session成功";
    }


    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        return "User-Agent"+userAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent) {
        return "User-Agent"+userAgent;
    }









}
